import request from '@/utils/request';
import { I_LoginForm, I_LoginRes } from '@/types';
import { I_PersonRes } from '../types/user';
export const loginUser = (data: I_LoginForm): Promise<I_LoginRes> => {
    return new Promise((_res, _rej) => {
        request<I_LoginRes>({
            url: `user/login`,
            method: "post",
            data
        }).then(res => {
            if (+res.code !== 200) {
                ElMessage.error({
                    message: res.msg,
                });
                return _rej(res.msg);
            }
            ElMessage.success({
                message: res.msg || '登录成功',
            });
            _res(res.data);
        }).catch((err: Error) => {
            console.log(err);
            ElMessage.error({
                message: '出现异常错误！',
            });
            _rej(err);
        });;
    });
};
export const getPersonInformation = (data: { id: number; }): Promise<I_PersonRes> => {
    return new Promise((_res, _rej) => {
        request<I_PersonRes>({
            url: `user/id/${data.id}`,
            method: "get",
        }).then(res => {
            if (+res.code !== 200) {
                ElMessage.error({
                    message: res.msg,
                });
                return _rej(res.msg);
            }
            ElMessage.success({
                message: res.msg || '获取信息成功',
            });
            _res(res.data);
        }).catch((err: Error) => {
            console.log(err);
            ElMessage.error({
                message: '出现异常错误！',
            });
            _rej(err);
        });;
    });
};
export const updatePersonInformation = (data: I_PersonRes): Promise<any> => {
    return new Promise((_res, _rej) => {
        request<any>({
            url: `user`,
            method: "post",
            data
        }).then(res => {
            if (+res.code !== 200) {
                ElMessage.error({
                    message: res.msg,
                });
                return _rej(res.msg);
            }
            ElMessage.success({
                message: res.msg || '更新成功',
            });
            _res(res.data);
        }).catch((err: Error) => {
            console.log(err);
            ElMessage.error({
                message: '出现异常错误！',
            });
            _rej(err);
        });;
    });
};
export const getAllUserInformation = (data: { index: number, size: number; }): Promise<{ total: number; records: I_PersonRes[]; }> => {
    return new Promise((_res, _rej) => {
        request<{ total: number; records: I_PersonRes[]; }>({
            url: `user/page?pageIndex=${data.index}&pageSize=${data.size}`,
            method: "get",
        }).then(res => {
            if (+res.code !== 200) {
                ElMessage.error({
                    message: res.msg,
                });
                return _rej(res.msg);
            }
            ElMessage.success({
                message: res.msg || '获取信息成功',
            });
            _res(res.data);
        }).catch((err: Error) => {
            console.log(err);
            ElMessage.error({
                message: '出现异常错误！',
            });
            _rej(err);
        });;
    });
};
export const deleteUserInformation = (data: { id: number; }): Promise<I_PersonRes[]> => {
    return new Promise((_res, _rej) => {
        request<I_PersonRes[]>({
            url: `user/${data.id}`,
            method: "delete",
        }).then(res => {
            if (+res.code !== 200) {
                ElMessage.error({
                    message: res.msg,
                });
                return _rej(res.msg);
            }
            ElMessage.success({
                message: res.msg || '删除成功',
            });
            _res(res.data);
        }).catch((err: Error) => {
            console.log(err);
            ElMessage.error({
                message: '出现异常错误！',
            });
            _rej(err);
        });;
    });
};
export const getPersonData = (data: { search: string, index: number, size: number; }): Promise<{ total: number; records: I_PersonRes[]; }> => {
    return new Promise((_res, _rej) => {
        request<{ total: number; records: I_PersonRes[]; }>({
            url: `user/search?searchInput=${data.search}&pageIndex=${data.index}&pageSize=${data.size}`,
            method: "get",
        }).then(res => {
            if (+res.code !== 200) {
                ElMessage.error({
                    message: res.msg,
                });
                return _rej(res.msg);
            }
            ElMessage.success({
                message: res.msg || '查询成功',
            });
            _res(res.data);
        }).catch((err: Error) => {
            console.log(err);
            ElMessage.error({
                message: '出现异常错误！',
            });
            _rej(err);
        });;
    });
};
export const addPersonData = (data: I_PersonRes): Promise<I_PersonRes> => {
    return new Promise((_res, _rej) => {
        request<I_PersonRes>({
            url: `user`,
            method: "post",
            data
        }).then(res => {
            if (+res.code !== 200) {
                ElMessage.error({
                    message: res.msg,
                });
                return _rej(res.msg);
            }
            ElMessage.success({
                message: res.msg || '新增成功',
            });
            _res(res.data);
        }).catch((err: Error) => {
            console.log(err);
            ElMessage.error({
                message: '出现异常错误！',
            });
            _rej(err);
        });;
    });
};
export const exportPersonData = (data: { institute?: string, major?: string; cls?: string; role?: string; }): Promise<I_PersonRes[]> => {
    return new Promise((_res, _rej) => {
        request<I_PersonRes[]>({
            url: `user/export?institute=${data.institute}&major=${data.major}&cls=${data.cls}&role=${data.role}`,
            method: "get",
            data
        }).then(res => {
            if (+res.code !== 200) {
                ElMessage.error({
                    message: res.msg,
                });
                return _rej(res.msg);
            }
            ElMessage.success({
                message: res.msg || '正在整合数据,即将导出下载...',
            });
            _res(res.data);
        }).catch((err: Error) => {
            console.log(err);
            ElMessage.error({
                message: '出现异常错误！',
            });
            _rej(err);
        });;
    });
};
export const importPersonData = (data: any): Promise<I_PersonRes[]> => {
    return new Promise((_res, _rej) => {
        request<I_PersonRes[]>({
            url: `user/import`,
            method: "post",
            data: data,
            headers: {
                "Content-Type": "multipart/form-data;charset=utf-8",
            }
        }).then(res => {
            if (+res.code !== 200) {
                ElMessage.error({
                    message: res.msg,
                });
                return _rej(res.msg);
            }
            ElMessage.success({
                message: res.msg || '正在整合数据,即将导出下载...',
            });
            _res(res.data);
        }).catch((err: Error) => {
            console.log(err);
            ElMessage.error({
                message: '出现异常错误！',
            });
            _rej(err);
        });;
    });
};
export { };